package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.AppAddHandlerRecordBo;
import org.dromara.system.domain.bo.AppAddWarehouseRecordBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppWarehouseRecordVo;
import org.dromara.system.domain.bo.AppWarehouseRecordBo;
import org.dromara.system.service.IAppWarehouseRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 入库记录
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/warehouseRecord")
public class AppWarehouseRecordController extends BaseController {

    private final IAppWarehouseRecordService appWarehouseRecordService;


    /**
     * 创建入库 (手动入库)
     */
    @SaCheckPermission("system:warehouseRecord:list")
    @GetMapping("/createWareHouse")
    public R<Void> createWareHouse(AppAddHandlerRecordBo appAddWarehouseRecordBo) {
        return appWarehouseRecordService.createWareHouse(appAddWarehouseRecordBo);
    }


    /**
     * 订单入库 (自动入库)
     */
    @SaCheckPermission("system:warehouseRecord:list")
    @GetMapping("/enterWarehouse")
    public R<Void> enterWarehouse(String orderId) {
        return appWarehouseRecordService.enterWarehouse(orderId);
    }



    /**
     * 查询入库记录列表
     */
    @SaCheckPermission("system:warehouseRecord:list")
    @GetMapping("/list")
    public TableDataInfo<AppWarehouseRecordVo> list(AppWarehouseRecordBo bo, PageQuery pageQuery) {
        return appWarehouseRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出入库记录列表
     */
    @SaCheckPermission("system:warehouseRecord:export")
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppWarehouseRecordBo bo, HttpServletResponse response) {
        List<AppWarehouseRecordVo> list = appWarehouseRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "入库记录", AppWarehouseRecordVo.class, response);
    }

    /**
     * 获取入库记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:warehouseRecord:query")
    @GetMapping("/{id}")
    public R<AppWarehouseRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appWarehouseRecordService.queryById(id));
    }

    /**
     * 新增入库记录
     */
    @SaCheckPermission("system:warehouseRecord:add")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppWarehouseRecordBo bo) {
        return toAjax(appWarehouseRecordService.insertByBo(bo));
    }

    /**
     * 修改入库记录
     */
    @SaCheckPermission("system:warehouseRecord:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppWarehouseRecordBo bo) {
        return toAjax(appWarehouseRecordService.updateByBo(bo));
    }

    /**
     * 删除入库记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:warehouseRecord:remove")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appWarehouseRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
